<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>房源详细信息</title>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css">
    <style type="text/css">
        .layui-table-cell {
            height: 100%;
            max-width: 100%;
        }
    </style>
</head>
<body>

<form class="layui-form">

    <div class="layui-inline">
        <div class="layui-input-inline">
            <input type="text" id="value" style="width: 330px" name="name" lay-verify="required" placeholder="请输入小区名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <button type="button" class="layui-btn" id="search">搜索</button>
</form>

<table id="roomTable" lay-filter="roomTable"></table>

<!-- 编辑页面开始 -->
<form class="layui-form" id="amendRoomForm" lay-filter="amendRoomForm" style="display: none;padding: 20px 55px 0px 0px">
    <input type="hidden" id="id" name="id" value="">
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">房源概括</label>
        <div class="layui-input-block">
            <textarea name="detail" placeholder="请输入房源概括内容" lay-verify="required" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" id="area-picker">
        <div class="layui-form-label">房源地址</div>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="province" id="province" class="province-selector" lay-verify="required" lay-filter="province-1">

            </select>
        </div>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="city" id="city" class="city-selector" lay-verify="required" lay-filter="city-1">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">详细地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" placeholder="请输入房源详细地址" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">周边交通</label>
        <div class="layui-input-block">
            <textarea name="traffic" placeholder="请输入房源周边交通" lay-verify="required" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">付款方式</label>
        <div class="layui-input-block">
            <input type="text" name="payType" placeholder="请输入房源付款方式" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">楼层</label>
        <div class="layui-input-block">
            <input type="text" name="floor" placeholder="请输入房源楼层信息" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">小区</label>
        <div class="layui-input-block">
            <input type="text" name="village" placeholder="请输入房源小区信息" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">装修</label>
        <div class="layui-input-block">
            <input type="text" name="renovation" placeholder="请输入房源装修情况" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">朝向</label>
        <div class="layui-input-block">
            <input type="text" name="orientation" placeholder="请输入房源朝向" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">住宅类型</label>
        <div class="layui-input-block">
            <input type="text" name="roomType" placeholder="请输入住宅类型"    lay-verify="required" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit  lay-filter="amendRoomSubmit">确认修改</button>
            <button type="button"  id="closeAmendRoom" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>
</form>
<!-- 编辑页面结束 -->



<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">

    //配置插件目录
    layui.config({
        base: 'layui/mods/'
        , version: '1.0'
    });

    layui.use(['table','form','layer','layarea'], function () {

        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var layarea = layui.layarea;
        table.render({
            elem:'#roomTable'
            ,url:'house-room-detail/list'
            ,page:true
            ,limit:10
            ,limits:[6,8,10,20]
            ,cols:[[
                {field:'roomId',title:'编号',sort:true,width:80,align:'center'}
                ,{field:'roomName',title:'房源名称',align:'center'}
                ,{field:'detail',title:'房源概括',align:'center'}
                ,{field:'province',title:'省份',align:'center'}
                ,{field:'city',title:'城市',align:'center'}
                ,{field:'address',title:'详细地址',align:'center'}
                ,{field:'traffic',title:'周边交通',align:'center'}
                ,{field:'payType',title:'付款方式',align:'center'}
                ,{field:'floor',title:'楼层',align:'center'}
                ,{field:'village',title:'小区',align:'center'}
                ,{field:'renovation',title:'装修',align:'center'}
                ,{field:'orientation',title:'朝向',align:'center'}
                ,{field:'roomType',title:'住宅类型',align:'center'}
                ,{toolbar: '#barDemo', align:'center'}
            ]]
        });
        /**
         * 监听roomTable的查看编辑删除按钮的单击事件
         * */
        table.on('tool(roomTable)',function (obj) {
            var data = obj.data;
            //编辑按钮的单击事件
            if(obj.event=='edit'){
                form.val("amendRoomForm",data);
                if (data.province ==null || data.city == null) {
                    layarea.render({
                        elem: '#area-picker',
                        data: {
                            province: '河南省',
                            city: '郑州市',
                        }
                    });
                }else {
                    if (checkProvince(data.province)) {
                        layarea.render({
                            elem: '#area-picker',
                            data: {
                                province: data.province+'省',
                                city: data.city,
                            }
                        });
                    }else {
                        layarea.render({
                            elem: '#area-picker',
                            data: {
                                province: data.province+'市',
                                city: data.city,
                            }
                        });
                    }
                }
                layer.open({
                    type:1,
                    title:['编辑房源详情','font-size:18px;color:orange;'],
                    area:['760px','80%'],
                    content:$("#amendRoomForm"),
                });
            }
        });

        /**
         * 监听修改页面的确认修改按钮
         * */
        form.on("submit(amendRoomSubmit)",function (obj) {
            layer.confirm("你确定要修改房源详情吗？",{icon:'3',title:'提示'},function () {
                $.ajax({
                    url:'house-room-detail/update',
                    data:obj.field,
                    type:"put",
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            obj.form.reset();
                            layer.closeAll();
                            table.reload("roomTable");
                            layer.msg("修改房源详情成功！",{icon:'1',title:'提示',closeBtn :'1'});
                        }
                    }
                });
            })
            return false;
        });
        /**
         * 各个页面的一些关闭按钮
         * */
        $("#closeAmendRoom").click(function () {
            layer.closeAll();
        })
        /**
         * 搜索功能
         * */
        $("#search").click(function () {
            if($("#value").val()==""){
                table.reload("roomTable",{
                    url:'house-room-detail/list',
                    page:{
                        curr:1
                    }
                });
            }else{
                table.reload("roomTable",{
                    url:'house-room-detail/search',
                    where:{
                        name:$("#value").val()
                    }
                });
            }
        });


        layarea.render({
            elem: '#area-picker',
            // data: {
            //     province: '河南',
            //     city: '郑州市',
            // },
            change: function (res) {
                //选择结果
                $("#province").val(res.province);
                $("#city").val(res.city);
            }
        });

        function checkProvince(province) {
            var flag = true;
            switch (province) {
                case "北京" : flag = false; break;
                case "上海" : flag = false; break;
                case "天津" : flag = false; break;
                case "重庆" : flag = false;
            }
            return flag;
        }

    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

</body>
</html>